package com.wmz.springcloud.controller;

import com.wmz.springCloud.common.CommonResult;
import com.wmz.springCloud.common.Const;
import com.wmz.springCloud.entity.Payment;
import com.wmz.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/payment")
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @Value("${server.port}")
    private String port;

    @PostMapping("/create")
    public CommonResult<Payment> create(@RequestBody Payment payment) {
        log.info("开始了。。。。");
        int i = paymentService.create(payment);
        if (i > 1) {
            return new CommonResult<>(Const.SUCCESS,Const.SuccessMessage+ "  port:  " + port);
        } else {
            return new CommonResult<>(Const.ERROR,Const.ErrorMessage);
        }
    }

    @GetMapping("/getPaymentById/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable Long id) {
        Payment payment = paymentService.getPaymentById(id);
        log.info("开始了。。。。");
        return new CommonResult<>(Const.SUCCESS,Const.SuccessMessage+ "  port:  " + port,payment);
    }

    @GetMapping("/getFeignOK/{id}")
    public CommonResult<Payment> getFeignOK(@PathVariable Long id) {
        Payment payment = paymentService.getPaymentById(id);
        log.info("开始了。。。。");
        return new CommonResult<>(Const.SUCCESS,Const.SuccessMessage+ "  port:  " + port,payment);
    }

    @GetMapping("/getFeignTimeOut/{id}")
    public CommonResult<Payment> getFeignTimeOut(@PathVariable Long id) {
        Payment payment = paymentService.getPaymentById(id);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("开始了。。。。");
        return new CommonResult<>(Const.SUCCESS,Const.SuccessMessage+ "  port:  " + port,payment);
    }
}
